package com.lyx.raindrop.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.lyx.framework.bmob.BmobManager;
import com.lyx.framework.helper.GlideHelper;
import com.lyx.raindrop.R;
import com.lyx.raindrop.constants.ChatConstants;
import com.lyx.raindrop.model.ChatModel;
import com.lyx.raindrop.model.ChatObjectModel;
import com.lyx.raindrop.ui.ImagePreviewActivity;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import de.hdodenhof.circleimageview.CircleImageView;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<ChatModel> mList;
    private Context mContext;
    private ChatObjectModel mChatObjectModel;
    private String meUserPhoto;

    public ChatAdapter(Context context, List<ChatModel> list, ChatObjectModel chatObjectModel) {
        this.mContext = context;
        this.mList = list;
        this.mChatObjectModel = chatObjectModel;
        meUserPhoto = BmobManager.getInstance().getUser().getPhoto();
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getType();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        if (viewType == ChatConstants.TYPE_LEFT_TEXT) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_chat_left_text, parent, false);
            viewHolder = new LeftTextViewHolder(view);
        } else if (viewType == ChatConstants.TYPE_RIGHT_TEXT) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_chat_right_text, parent, false);
            viewHolder = new RightTextViewHolder(view);
        } else if (viewType == ChatConstants.TYPE_LEFT_IMAGE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_chat_left_img, parent, false);
            viewHolder = new LeftImageViewHolder(view);
        } else if (viewType == ChatConstants.TYPE_RIGHT_IMAGE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_chat_right_img, parent, false);
            viewHolder = new RightImageViewHolder(view);
        } else if (viewType == ChatConstants.TYPE_LEFT_LOCATION) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_chat_left_location, parent, false);
            viewHolder = new LeftLocationViewHolder(view);
        } else if (viewType == ChatConstants.TYPE_RIGHT_LOCATION) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_chat_right_location, parent, false);
            viewHolder = new RightLocationViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_chat_other, parent, false);
            viewHolder = new OtherViewHolder(view);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final ChatModel model = mList.get(position);
        if (holder instanceof LeftTextViewHolder) {
            ((LeftTextViewHolder) holder).leftText.setText(model.getText());
            GlideHelper.loadPicture(mContext, mChatObjectModel.getPhoto(), ((LeftTextViewHolder) holder).leftPhoto);
        } else if (holder instanceof RightTextViewHolder) {
            ((RightTextViewHolder) holder).rightText.setText(model.getText());
            GlideHelper.loadPicture(mContext, meUserPhoto, ((RightTextViewHolder) holder).rightPhoto);
        } else if (holder instanceof LeftImageViewHolder) {
            GlideHelper.loadPicture(mContext, mChatObjectModel.getPhoto(), ((LeftImageViewHolder) holder).leftPhoto);
            if (model.getImgUrl() != null) GlideHelper.loadPicture(mContext, model.getImgUrl(), ((LeftImageViewHolder) holder).leftImg);
            ((LeftImageViewHolder) holder).leftImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtils.show("onclick");
                }
            });
        } else if (holder instanceof RightImageViewHolder) {
            GlideHelper.loadPicture(mContext, meUserPhoto, ((RightImageViewHolder) holder).rightPhoto);
            if (model.getLocalFile() != null) {
                GlideHelper.loadFile(mContext, model.getLocalFile(), ((RightImageViewHolder) holder).rightImg);
                ((RightImageViewHolder) holder).rightImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImagePreviewActivity.startActivity(mContext, false, model.getLocalFile().getPath());
                    }
                });
            } else {
                GlideHelper.loadPicture(mContext, model.getImgUrl(), ((RightImageViewHolder) holder).rightImg);
                ((RightImageViewHolder) holder).rightImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImagePreviewActivity.startActivity(mContext, true, model.getImgUrl());
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    static class LeftTextViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView leftPhoto;
        private TextView leftText;

        public LeftTextViewHolder(@NonNull View itemView) {
            super(itemView);
            leftPhoto = itemView.findViewById(R.id.iv_left_photo);
            leftText = itemView.findViewById(R.id.tv_left_text);
        }

    }

    static class RightTextViewHolder extends RecyclerView.ViewHolder {

        private TextView rightText;
        private CircleImageView rightPhoto;

        public RightTextViewHolder(@NonNull View itemView) {
            super(itemView);
            rightText = itemView.findViewById(R.id.tv_right_text);
            rightPhoto = itemView.findViewById(R.id.iv_right_photo);
        }

    }

    static class LeftImageViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView leftPhoto;
        private ImageView leftImg;

        public LeftImageViewHolder(@NonNull View itemView) {
            super(itemView);
            leftPhoto = itemView.findViewById(R.id.iv_left_photo);
            leftImg = itemView.findViewById(R.id.iv_left_img);
        }

    }

    static class RightImageViewHolder extends RecyclerView.ViewHolder {

        private ImageView rightImg;
        private CircleImageView rightPhoto;

        public RightImageViewHolder(@NonNull View itemView) {
            super(itemView);
            rightImg = itemView.findViewById(R.id.iv_right_img);
            rightPhoto = itemView.findViewById(R.id.iv_right_photo);
        }

    }

    static class LeftLocationViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView leftPhoto;
        private ImageView leftLocationImg;
        private TextView leftAddress;

        public LeftLocationViewHolder(@NonNull View itemView) {
            super(itemView);
            leftPhoto = itemView.findViewById(R.id.iv_left_photo);
            leftLocationImg = itemView.findViewById(R.id.iv_left_location_img);
            leftAddress = itemView.findViewById(R.id.tv_left_address);
        }

    }

    static class RightLocationViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_right_location_img;
        private TextView tv_right_address;
        private CircleImageView iv_right_photo;

        public RightLocationViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_right_location_img = itemView.findViewById(R.id.iv_right_location_img);
            tv_right_address = itemView.findViewById(R.id.tv_right_address);
            iv_right_photo = itemView.findViewById(R.id.iv_right_photo);
        }

    }

    static class OtherViewHolder extends RecyclerView.ViewHolder {

        public OtherViewHolder(View itemView) {
            super(itemView);
        }

    }


}
